package ltd.newbee.mall.config;

import ltd.newbee.mall.entity.AdminUser;
import ltd.newbee.mall.service.AdminUserService;
import ltd.newbee.mall.util.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Service
public class CurrentAdminUser {

    private static final Logger LOGGER = LoggerFactory.getLogger(CurrentAdminUser.class);

    @Resource
    private TokenAdminUser tokenAdminUser;

    @Resource
    private AdminUserService adminUserService;

    public AdminUser currentLoginUser() {
        HttpServletRequest request = SpringUtils.currentRequest();
        String token = request.getHeader(tokenAdminUser.getHeader());
        LOGGER.info("当前用户 token = {}", token);
        AdminUser user = adminUserService.selectByToken(token);
        LOGGER.info("当前用户 = {} ", user);
        return user;
    }
}
